import requests
import base64
import urllib

#获取token
res = requests.get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=xjy5qSR7IzPnvjju6Gc0fQxH&client_secret=zAgeiB0o3S8prsqmRZFHqcmeB8ybRcc2")
# print(res.json())

token = res.json()['access_token']

#接口地址
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token='+token

# 定义头部
myheaders = {'Content-Type':'application/x-www-form-urlencoded'}

#读取图片
myimg = open('mydjango\code1.png','rb')
temp_img = myimg.read()
myimg.close()

#进行base64编码
temp_data = {'image':base64.b64encode(temp_img)}

#对图片地址进行urlencode操作
temp_data = urllib.parse.urlencode(temp_data)

#请求识图接口
res = requests.post(url=url,data=temp_data,headers=myheaders)

print(res.json())